//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.ObjectBuilder;
using EntLibContrib.Resource.Providers;

namespace EntLibContrib.Resource.Configuration
{
	/// <summary>
	/// Configuration information for DataResourceProviderData. This class represents the extra information, over and
	/// above what is defined in <see cref="ResourceProviderData" />, needed to connect resource to the Data block.
	/// </summary>
	[Assembler(typeof(DataResourceProviderAssembler))]
	public class DataResourceProviderData : ResourceProviderData
	{
		#region Constants
		private const string databaseInstanceNameProperty = "databaseInstanceName";
		#endregion

		#region Properties
		/// <summary>
		/// Name of the database instance to use for resources. Instance must be defined in Data configuration.
		/// </summary>
		[ConfigurationProperty(databaseInstanceNameProperty, IsRequired = true)]
		public string DatabaseInstanceName
		{
			get { return (string)base[databaseInstanceNameProperty]; }
			set { base[databaseInstanceNameProperty] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="DataResourceProviderData"/> class.
		/// </summary>
		public DataResourceProviderData ()
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="DataResourceProviderData"/> class.
		/// </summary>
		/// <param name="name">The name of the <see cref="DataResourceProviderData"/>.</param>
		/// <param name="databaseInstanceName">Name of the database instance to use for resources. Instance must be defined in Data configuration.</param>
		/// <param name="resourceBaseName">Name of the resource base.</param>
		public DataResourceProviderData(string name, string databaseInstanceName, string resourceBaseName)
			: base(name, resourceBaseName, typeof(DataResourceProvider))
		{
			this.DatabaseInstanceName = databaseInstanceName;
		}
		#endregion
	}

	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to build an <see cref="DataResourceProvider"/> described by a <see cref="DataResourceProviderData"/> configuration object.
	/// </summary>
	/// <remarks>This type is linked to the <see cref="ResourceProviderData"/> type and it is used by the <see cref="ResourceProviderCustomFactory"/> 
	/// to build the specific <see cref="IResourceProvider"/> object represented by the configuration object.
	/// </remarks>	
	public class DataResourceProviderAssembler : IAssembler<IResourceProvider, ResourceProviderData>
	{
		#region Public Methods
		/// <summary>
		/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
		/// Builds a <see cref="DataResourceProvider"/> based on an instance of <see cref="DataResourceProviderData"/>.
		/// </summary>
		/// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
		/// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="DataResourceProviderData"/>.</param>
		/// <param name="configurationSource">The source for configuration objects.</param>
		/// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
		/// <returns>
		/// A fully initialized instance of <see cref="DataResourceProvider"/>.
		/// </returns>
		/// <seealso cref="ResourceProviderCustomFactory"/>
		public IResourceProvider Assemble (IBuilderContext context, ResourceProviderData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
		{
			if (context == null)
				throw new ArgumentNullException("context");

			if (objectConfiguration == null)
				throw new ArgumentNullException("objectConfiguration");

			DataResourceProviderData castedObjectConfiguration = (DataResourceProviderData)objectConfiguration;
			Database database = (Database)context.HeadOfChain.BuildUp(context, typeof(Database), null, castedObjectConfiguration.DatabaseInstanceName);

			IResourceProvider createdObject = new DataResourceProvider(database, castedObjectConfiguration.ResourceBaseName);

			return createdObject;
		}
		#endregion
	}
}